package com.example.lin.day9_27.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lin.day9_27.R;
import com.example.lin.day9_27.mvp.presenter.MainPresenter;
import com.example.lin.day9_27.mvp.view.IMainView;
import com.example.lin.day9_27.pojo.ResultData;
import com.example.lin.day9_27.pojo.User;
import com.example.lin.day9_27.utils.ApiRequestService;
import com.example.lin.day9_27.utils.RetrofitClient;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;


public class MainActivity extends AppCompatActivity implements IMainView {

    @BindView(R.id.et_user)
    EditText etUser;
    @BindView(R.id.et_psw)
    EditText etPsw;
    @BindView(R.id.btn_show)
    Button btnShow;
    @BindView(R.id.tv_show)
    TextView tvShow;

    //View 持有 Presenter 的引用
    private MainPresenter mainPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        mainPresenter = new MainPresenter(this);


    }

    @OnClick(R.id.btn_show)
    public void onViewClicked() {
        //发起请求
        String psw = etPsw.getText().toString();
        String user = etUser.getText().toString();
        mainPresenter.login(user, psw);
    }


    /**
     * 将用户信息 更新到 界面上
     *
     * @param user
     */
    @Override
    public void updateUserInfo(User user) {
        tvShow.setText(user.toString());
    }

    /**
     * 处理错误的提示操作
     *
     * @param status
     * @param msg
     */
    @Override
    public void showErrorInfo(int status, String msg) {
        if (status == 0) {
            Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        } else if (status == 1) {
//            new AlertDialog.Builder()
        }
    }



}
